import Sortable from "sortablejs";
export const sortMixin = {
  data: function() {
    return {
      sortData: [] // 拖拽数据
    };
  },
  computed: {},

  methods: {
    // 拖拽初始化
    setSort(list) {
      this.sortData = [...list];
      const el = this.$refs.table.$el.querySelectorAll(
        ".el-table__body-wrapper > table > tbody"
      )[0];
      
      this.sortable = Sortable.create(el, {
        ghostClass: "sortable-ghost",
        dragClass: "dragClass",
        group: { name: "table", pull: true, put: true },
        animation: 150,
        // forceFallback: true,
        setData: function (dataTransfer) {
          dataTransfer.setData("Text", "");
        },
        onEnd: ({ newIndex, oldIndex }) => {
          const val = this.sortData[oldIndex];
          this.sortData.splice(oldIndex, 1);
          this.sortData.splice(newIndex, 0, val);
        }
      });
    }
  }
};